<?php session_start();
include_once("Classes/PHPExcel.php");
include "constants.php";
$inmail=INEMAIL;
$NEWPROCESS=NEWPROCESS;
$CCEMAILS=CCEMAILS;

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 

if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db(DB_NAME);

/*
$results is result set from querying db
*/
function writeExcel($resource, $fileName){
	$excel = new PHPExcel();
        $excel->removeSheetByIndex(0);
	$excel->createSheet();
	$excel->getActiveSheet()->setTitle('MAIN');

	//$excel->setActiveSheetIndex(0);
	$lineNum = 2;

	$excel->getActiveSheet()->setCellValue('A1','ID');
	while ($row = mysql_fetch_array($resource)){
		for ($i = 0; $i < count($row)/2; $i++){
			$excel->getActiveSheet()->setCellValue(number2Letter($i+1,1).$lineNum,$row[$i]);
		}
		
		$lineNum++;
	}
	
	$writer = PHPExcel_IOFactory::createWriter($excel,"Excel2007");
	$writer->save($fileName);
}

function number2Letter($num, $round){
	$letters = array(
		'1' => 'A',
		'2' => 'B',
		'3' => 'C',
		'4' => 'D',
		'5' => 'E',
		'6' => 'F',
		'7' => 'G',
		'8' => 'H',
		'9' => 'I',
		'10' => 'G',
		'11' => 'K',
		'12' => 'L',
		'13' => 'M',
		'14' => 'N',
		'15' => 'O',
		'16' => 'P',
		'17' => 'Q',
		'18' => 'R',
		'19' => 'S',
		'20' => 'T',
		'21' => 'U',
		'22' => 'V',
		'23' => 'W',
		'24' => 'X',
		'25' => 'Y',
		'26' => 'Z'
	);
	
	if ($num > 26){
		if (intval($num / 26) > 26){
			number2Letter(intval($num / 26), $round++);
		}
		else {
			if ($num % 26 != 0){
				return $letters[intval($num / 26)].$letters[$num % 26];
			}
			else {
				return $letters[intval($num / 26)].'Z';
			}
		}
	}
	else {
		return $letters[$num];
	}
}

function main(){
	// check duplicated email
	$query = "SELECT * FROM Profiles limit 100";
	$resource = mysql_query($query);

	if ($resource == false){		
		print('profiles retrieving failed.');
		exit();
	}
	
	$filePath = writeExcel($resource,'first1.xlsx');
	//downloadExcel($filePath);ssss
}

main();

?>
<head>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Cash2u.ca: Payday loan Application process</title>
<META NAME="Description" CONTENT=Cash2u.ca offers online payday loans and cash advance for Canadians">
</head>
<body>
testing plus excel main function
</body>
</html>
